import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/Login";
import Home from "@/views/Home";
import Welcome from "@/views/Welcome";
import User from "@/components/user/User";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    redirect: './welcome',
    name: 'Home',

    component: Home,
    children:[
      {
        path: '/welcome',
        name: 'Welcome',
        component: Welcome
      },
      {
        path: '/users',
        name: 'User',
        component: User
      },
      {
        path: '/rights',
        name: 'Rights',
        component: () => import('@/components/power/Rights')
      },
      {
        path: '/roles',
        name: 'Roles',
        component: () => import('@/components/power/Roles')
      },
    ]
  }
]

const router = new VueRouter({
  routes
})
//设置路由守卫
router.beforeEach((to,from,next)=>{
  //判断是否是登录请求
  if(to.path === '/login')
    next()
  //如果不是
  //取出token
  const myToken = window.sessionStorage.getItem('token')
  //判断token是否为空 为空跳转到登录页面
  if(!myToken)
    next('/login')
  //不为空则放行
  next()
})

export default router
